<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */

namespace app\home\controller;


use app\admin\model\Members;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Base extends Controller
{

    public function _initialize()
    {

        $request = Request::instance();
        $controllerName = $request->controller();
        $this->assign('controllerName',strtolower($controllerName));
    }

    /**
     * @param $cate
     * 获取下一期的开奖时间
     */
    public function get_next($cate){
        $now_time = date('H:i:s');
        $data = Db::name('cate_time')
            ->field('stage,dateline,open')
            ->where('cate',$cate)
            ->where('dateline','>',$now_time)
            ->order('id asc')
            ->find();
        switch ($cate){
            case 1:
                $back = $data;
                $start = strtotime(date('Y-m-d').' 09:05:16');
                $end = strtotime(date('Y-m-d').' 23:55:14');
                if(time()<$start ){
                    $last_sage = Db::name('at_eight')->order('id desc')->value('stage');
                    $back['stage'] =$last_sage+1;
                    $back['dateline'] = date('Y-m-d') .' 09:05:16';
                }elseif(time()>$end){
                    $last_sage = Db::name('at_eight')->order('id desc')->value('stage');
                    $back['stage'] =$last_sage+1;
                    $yesterday = strtotime('+1 day');
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 09:05:16';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['open'] = date('Y-m-d').' '.$back['open'];
                }
                break;
            case 2:
                $back = $data;
                $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                $back['open'] = date('Y-m-d').' '.$back['open'];
                break;
            case 3:
                $back = $data;
                $start = strtotime(date('Y-m-d').' 09:07:46');
                $end = strtotime(date('Y-m-d').' 23:57:46');
                if(time()<$start ){
                    $last_sage = Db::name('at_car')->order('id desc')->value('stage');
                    $back['stage'] =$last_sage+1;
                    $back['dateline'] = date('Y-m-d') .' 09:07:46';
                }elseif(time()>$end){
                    $last_sage = Db::name('at_car')->order('id desc')->value('stage');
                    $back['stage'] =$last_sage+1;
                    $yesterday = strtotime('+1 day');
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 09:07:46';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['open'] = date('Y-m-d').' '.$back['open'];
                }
                break;

            case 4:
                $back = $data;
                $change = strtotime(date('Y-m-d').' 04:04:33');
                if(time()<$change){
                    $yesterday = strtotime('-1 day');
                    $back['stage'] = date('Ymd',$yesterday).$back['stage'];
                }else {
                    $back['stage'] = date('Ymd') . $back['stage'];
                }
                $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                $back['open'] = date('Y-m-d').' '.$back['open'];
                break;
            case 5:
                $back = $data;
                $change = strtotime(date('Y-m-d').' 00:01:00');
                if(time()<$change){
                    $yesterday = strtotime('-1 day');
                    $back['stage'] = date('Ymd',$yesterday).$back['stage'];
                }else {
                    $back['stage'] = date('Ymd') . $back['stage'];
                }
                $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                $back['open'] = date('Y-m-d').' '.$back['open'];
                break;
            case 6:
                $back = $data;
                $start = strtotime(date('Y-m-d').' 09:10:38');
                $end = strtotime(date('Y-m-d').' 23:04:46');
                if(time()<$start ){
                    $back['stage'] =date('Ymd').'001';
                    $back['dateline'] = date('Y-m-d') .' 09:10:38';
                }elseif(time()>$end){
                    $yesterday = strtotime('+1 day');
                    $back['stage'] =date('Ymd',$yesterday).'001';
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 09:10:38';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['stage'] =date('Ymd').$back['stage'];
                }
                break;
            case 7:
                $back = $data;
                $start = strtotime(date('Y-m-d').' 09:12:03');
                $end = strtotime(date('Y-m-d').' 23:02:11');
                if(time()<$start ){
                    $back['stage'] =date('Ymd').'01';
                    $back['dateline'] = date('Y-m-d') .' 09:12:03';
                }elseif(time()>$end){
                    $yesterday = strtotime('+1 day');
                    $back['stage'] =date('Ymd',$yesterday).'01';
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 09:12:03';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['stage'] =date('Ymd').$back['stage'];
                }
                break;
            case 8:
                $back = $data;
                $back['stage'] = date('ymd').$back['stage'];
                break;
            case 9:

                $back = $data;
                $start = strtotime(date('Y-m-d').' 08:41:09');
                $end = strtotime(date('Y-m-d').'  22:11:48');
                if(time()<$start ){
                    $back['stage'] =date('ymd').'001';
                    $back['dateline'] = date('Y-m-d') .' 08:41:09';
                }elseif(time()>$end){
                    $yesterday = strtotime('+1 day');
                    $back['stage'] =date('ymd',$yesterday).'001';
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 08:41:09';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['stage'] =date('ymd').$back['stage'];
                }
                break;
            case 10:
                $back = $data;
                $start = strtotime(date('Y-m-d').' 09:11:27');
                $end = strtotime(date('Y-m-d').'  23:02:12');
                if(time()<$start ){
                    $back['stage'] =date('ymd').'01';
                    $back['dateline'] = date('Y-m-d') .' 09:11:27';
                }elseif(time()>$end){
                    $yesterday = strtotime('+1 day');
                    $back['stage'] =date('ymd',$yesterday).'01';
                    $back['dateline'] = date('Y-m-d',$yesterday) .' 09:11:27';
                }else{
                    $back['dateline'] = date('Y-m-d').' '.$back['dateline'];
                    $back['stage'] =date('ymd').$back['stage'];
                }
                break;
        }
        return $back;
    }
    /**
     * 定义方法
     *
     * 重写thinkphp跳转成功方法
     *
     * @param string $message
     * @param string $uri
     * @param bool|mixed $param
     */
    public function success_new($message,$uri="",$param = [])
    {
        if ($uri) {
            $param_str = '';
            if($param) {
                foreach ($param as $k => $v) {
                    $param_str .= '/' . $k . '/' . $v;
                }
                echo '<script>alert(\'' . $message . '\');location=\'' . $uri . $param_str . '\'</script>';
            }else{
                echo '<script>alert(\'' . $message . '\');location=\'' . $uri .  '\'</script>';
            }
        } else {
            echo '<script>alert(\''.$message.'\');history.go(-1)</script>';
        }
    }
    /**
     * 定义方法
     *
     * 重写thinkphp跳转失败方法
     *
     * @param string $message
     * @param string $uri
     * @param bool|mixed $param
     */
    public function error_new($message,$uri ="",$param = [])
    {
        if ($uri) {
            if($param) {
                $param_str = '';
                foreach ($param as $k => $v) {
                    $param_str .= '/' . $k . '/' . $v;
                }
                echo '<script>alert(\'' . $message . '\');location=\'' . $uri . $param_str . '\'</script>';
            }else{
                echo '<script>alert(\'' . $message . '\');location=\'' . $uri .  '\'</script>';
            }
        } else {
            echo '<script>alert(\''.$message.'\');history.go(-1)</script>';
        }
        exit;
    }
    /**
     * 上传一张图片
     */
    public function upload_one(){
        $file = request()->file('file');
        if(!$file){
            $data = array("status" =>0,"error" => '请选择上传图片');
            echo json_encode($data);
            exit;
        }
        $path = ROOT_PATH . 'public' . DS . 'uploads'. DS .'cate_img';
        $info = $file->move($path);
        if($info){
            $picd = 'cate_img/'.$info->getSaveName();
            $pic = Config::get('img_url').$picd;
            $data = array("status" =>1,"pic" => $pic,'picd'=>$picd);
            echo json_encode($data);
            exit;
        }else{
            $data = array("status" =>0,"error" => '上传图片失败');
            echo json_encode($data);
            exit;
        }
    }
    /**
     * 获取首页广告
     */
    public function get_ads(){
        $data = Db::name('advertise')->select();
        foreach ($data as $k=>$v){
            $data[$k]['ad_url'] =  Config::get('img_url').$v['ad_url'];
        }
        return $data;
    }
}